<?php
/**
 * 合作餐厅管理
 * @author shitou
 *
 */
class OutletAction extends CommonAction {
    
    /**
     * 餐厅管理
     */
    public function index(){

    	if(IS_AJAX){
    		$desc    = $this->_param("orderDirection","trim",   "desc");;
    		$order   = $this->_param("orderName",     "trim",   "addtime");
    		$pageNum = $this->_param("pageNum",       "intval",  C('PAGE_NUMS'));
    		$nowPage = $this->_param("nowPage",       "intval",  1);
    		$where   = $_POST['where'];
    		$cateid  = $this->_param("cateid","intval",0);
			
    		$map   = array("where"=>$where,"pageNum"=>$pageNum, "nowPage"=>$nowPage, "orderDirection"=>$desc ,"orderName"=>$order);
			$role  = session(C('USER_AUTH_ROLE'));
			if(C('ADMIN_ROLE')==session(C('USER_AUTH_ROLE'))){
				$map['noCheckRole'] = 1;
			} 
    		
    		$json  = sendRest("BDC/Outlet/index",$map);
    		$data  = $json['data'];
    		$count = $data['count'];
    		
    		//地址列表
    		$dict_address = sendRest("BDC/Outlet/dictAddress");
    		$this->assign("json",$dict_address['data']['zh-cn']['array']);
    		
    		$totalPage    = ceil($count/$pageNum);
    		$jsonData     = array('page'=>$nowPage,'total'=>$count,'totalPage'=>$totalPage);
    		
    		foreach($data['list'] AS $row){

    			$city     = $dict_address['data']['zh-cn']['array'][$row['city']][0];
    			$province = $dict_address['data']['zh-cn']['array'][$row['area4']][0];
    			$status   = $row['is_tingye']==1?"停业":"开业";
    			$exempt   = $row['is_exempt']==1?"是":"否";
    			$tigong   = $row['is_tigong']==1?"是":"否";
    			
    			$entry = array('id'=>$row['id'],
    				'cell'=>array(
    					'id'       => $row['id'],    //id
    					'title'    => $row['title'], //名称
    					'tel'      => $row['tel'],   //电话
    					'email'    => $row['email'], //电邮
    					'level'    => $row['level'], //星级
    					'city'     => $city.$province, //星级
    					'tigong'   => $tigong,
    					'status'   => $status,
    					'exempt'   => $exempt,
    					'type'     => $row['type']==1?"加盟店":"直营店",  //类型
    					'isdel'    => $row['isdel'], //id
    					'addtime'  => date('Y-m-d',$row['addtime']),
    				),
    			);
    			$jsonData['rows'][] = $entry;
    		}
    		$this->ajaxReturn($jsonData,"JSON");
    		
    	}else{
    		
    		//地址列表
    		$dict_address = sendRest("BDC/Outlet/dictAddress");
    		$this->assign("json",$dict_address['data']['zh-cn']['array']);
    		
    		//所有菜系
    		$cuisine = sendRest("BDC/Cuisine/view");
    		$this->assign("cuisine",$cuisine['data']);
    		
    		$this->display();
    	}
    }
    
    /**
     * 添加餐厅
     */
    public function add(){
    	
    	if(IS_POST){
    		$data   = sendRest("BDC/Outlet/add",$_POST,"post");
    		$this->ajaxReturn($data);
    	}else{
    		//所有菜系
    		$cuisine = sendRest("BDC/Cuisine/view");
    		$this->assign("cuisine",$cuisine['data']);
    		
    		//地址列表
    		$dict_address = sendRest("BDC/Outlet/dictAddress");
    		$this->assign("json",$dict_address['data']['zh-cn']['array']);
    		$this->assign("jsonen",$dict_address['data']['en-us']['array']);
    		$this->assign('UPLOAD_PATH',UPLOAD_PATH);
    		$this->display();
    	}
    }
    
    /**
     * 修改餐厅
     */
    public function edit(){
    	
    	if(IS_POST){
    		$data = sendRest("BDC/Outlet/edit",$_POST,"post");
    		$this->ajaxReturn($data);
    	}else{
    		//餐厅详情
    		$map['id'] = $this->_param("id","intval",0);
    		$data = sendRest("BDC/Outlet/detail",$map);
    		
    		//所有菜系
    		$cuisine = sendRest("BDC/Cuisine/view");
    		$this->assign("cuisine",$cuisine['data']);
    		
    		//选择的菜系数组
    		$cuisineArr = explode(',',$data['data']['cuisine']);
    		$this->assign("cuisineArr",$cuisineArr);
    		
    		//选择的免费配送时间-星期
    		$weekArr = explode(',',$data['data']['week']);
    		$this->assign("weekArr",$weekArr);

    		//地址列表
    		$dict_address = sendRest("BDC/Outlet/dictAddress");
    		$this->assign("json",$dict_address['data']['zh-cn']['array']);
    		$this->assign("jsonen",$dict_address['data']['en-us']['array']);
    		
    		//查询城市数据字典的级别
    		if($data['data']['city']){
    			$dict = D("Dict");
    			$row  = $dict->find($data['data']['area']);
    			$this->assign("row",$row);
    		}
    		
    		$this->assign('UPLOAD_PATH',UPLOAD_PATH);
    		$this->assign("detail",$data['data']);
			
			$module    = "Outlet";
			$thumb    = find_thumb($data['data']['solevar'],'thumb',$module);
			$this->assign("thumb",$thumb);
			
			$cjthumb    = find_thumb($data['data']['solevar'],'cjthumb',$module);
			$this->assign("cjthumb",$cjthumb);
			
    		$this->display();
    	}
    }
    
    /**
     * 删除餐厅
     */
    public function del(){
    	
    	$map['val'] = $this->_param("val","trim","");
    	$data = sendRest("BDC/Outlet/del",$map);
    	
		$this->ajaxReturn($data);
    }
    
    /**
     * 添加标注信息点
     */
    public function outletLbs(){
    	 
    	$map['id']      = $this->_param("id","intval",0);
    	$map['address'] = $this->_param("address","trim","");
    	 
    	$data = sendRest("BDC/Outlet/outletLbs",$map);
    	 
    	$this->assign("row",$data['data']['row']);
    	$this->assign("address",$data['data']['address']);
    	 
    	$this->display();
    }
    
    /**
     * 导出餐厅
     */
    public function export(){
    	$model = D("Outlet");
    	
    	$map['keywords'] = $this->_param("keywords","trim","");
    	$map['type']   = $this->_param("type","intval",0);
    	$map['level']   = $this->_param("level","intval",0);
    	$map['area2']   = $this->_param("area2","intval",0);
    	$map['area3']   = $this->_param("area3","intval",0);
    	$map['area4']   = $this->_param("area4","intval",0);
    	$map['cuisine']   = $this->_param("cuisine","intval",0);

    	 
    	$json  = sendRest("BDC/Outlet/index",$map);
    	$list  = $json['data']['list'];
    	
    	foreach($list as $k=>$v){
    		if($v['type']==1){
    			$list[$k]['type'] = "直营店";
    		}else{
    			$list[$k]['type'] = "加盟店";
    		}
    	}
    	
    	$model->export($list);
    }
    
    /**
     * 留言
     */
    public function message(){
    	
    	if(IS_AJAX){
    		
    		$desc    = $this->_param("orderDirection","trim",   "desc");;
    		$order   = $this->_param("orderName",     "trim",   "addtime");
    		$pageNum = $this->_param("pageNum",       "intval",  C('PAGE_NUMS'));
    		$nowPage = $this->_param("nowPage",       "intval",  1);
    		$where   = $_POST['where'];
    			
    		$map   = array("where"=>$where,"pageNum"=>$pageNum, "nowPage"=>$nowPage, "orderDirection"=>$desc ,"orderName"=>$order);

    		$mesg     = D("Message");
    		$data     = $mesg->mesgselect($map);
    		$count    = $data['count'];
    		
    		$totalPage    = ceil($count/$pageNum);
    		$jsonData     = array('page'=>$nowPage,'total'=>$count,'totalPage'=>$totalPage);
    		
    		foreach($data['list'] AS $row){
    			
    			$entry = array('id'=>$row['id'],
    				'cell'=>array(
    					'id'       => $row['id'],       //id
    					'email'    => $row['email'],    //邮箱
    					'mobile'   => $row['mobile'],   //电话
    					'content'  => $row['content'],  //留言内容
    					'tableid'  => $row['tableid']=="Guest"?"游客":"会员",  //星级
    					'addtime'  => date('Y-m-d',$row['addtime']),
    				),
    			);
    			$jsonData['rows'][] = $entry;
    		}
    		$this->ajaxReturn($jsonData,"JSON");
    		
    	}else{
    		$json = sendRest("BDC/Outlet/view/");
    		$this->assign("mesList",$json['data']);
    		$this->display();
    	}
    }
    
    //删除留言
    public function messageDel(){
    	
    	if(IS_AJAX){
    		
    		$map['id'] = $this->_param("val","trim","");
    		
    		$mesg = D("Message");
    		$data = $mesg->msgDel($map);
    		if($data){
    			$this->ajaxReturn(array("status"=>1,  "info"=>"删除成功！"));
    		}else{
    			$this->ajaxReturn(array("status"=>-1, "info"=>"删除失败！"));
    		}
    	}
    }
    
    //批量删除留言
    public function messageDelall(){
    	 
    	if(IS_AJAX){
    
    		$val = $this->_param("val","trim","");
    		$mesg = D("Message");
    		$data = $mesg->msgDelall($val);
    		if($data){
    			$this->ajaxReturn(array("status"=>1,  "info"=>"删除成功！"));
    		}else{
    			$this->ajaxReturn(array("status"=>-1, "info"=>"删除失败！"));
    		}
    	}
    }
    
    //开业停业
    public function business(){
    	
    	$map['val']  = $this->_param("val");
    	$map['type'] = $this->_param("type","intval",0);
    	
    	$json = sendRest("BDC/Outlet/business",$map);
    	$this->ajaxReturn($json);
    }
    
    //批量开启关闭免费配送
    public function distribution(){
    	
    	$map['val'] = $this->_param("val");
    	$map['rel'] = $this->_param("rel","trim","");
    	 
    	$json = sendRest("BDC/Outlet/distribution",$map);
    	$this->ajaxReturn($json);
    }
   
}